<template>
  <!-- 授权用户 -->
  <el-dialog title="选择用户" :visible.sync="visible" width="1350px" top="5vh" append-to-body>

    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" inline>
      <el-form-item label="作业项目" prop="projectName">
            <el-input size="small" v-model="queryParams.projectName" placeholder="请输入作业项目" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="井号" prop="wellNo">
            <el-input size="small" v-model="queryParams.wellNo" placeholder="请输入井号" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="井别" prop="wellType">
            <el-input size="small" v-model="queryParams.wellType" placeholder="请输入井别" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="开工日期" prop="workStartTime">
            <el-date-picker size="small" v-model="queryParams.workStartTime" type="date" placeholder="选择日期"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="resetQuery">重置</el-button>
            <el-button size="small" type="primary" @click="handleQuery">查询</el-button>
          </el-form-item>
    </el-form>


    <el-table ref="reportTable" v-loading="loading" :data="dataList" fit border @selection-change="handleSelectionChange"
      height="500">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="施工单位" prop="constructionOrganization" width="150" />
      <el-table-column label="井号" prop="wellNo" width="120" />
      <el-table-column label="井别" prop="wellType" width="100" />
      <el-table-column label="作业项目" prop="projectName" width="150">
        <template slot-scope="scope">
          <el-popover placement="top-start" width="400" trigger="hover">
            <div>
              <div v-for="(str, index) in str2List(scope.row.projectName)" :key="index">
                {{ index + 1 }}、{{ str }}
              </div>
            </div>
            <div class="text-overflow-ellipsis pointer" slot="reference">{{ scope.row.projectName }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="风险因素" prop="riskFactor" min-width="100">
        <template slot-scope="scope">
          <el-popover placement="top-start" width="800" trigger="hover">
            <div>
              <div v-for="(str, index) in str2List(scope.row.riskFactor)" :key="index">
                {{ index + 1 }}、{{ str }}
              </div>
            </div>
            <div class="text-overflow-ellipsis pointer" slot="reference">{{ scope.row.riskFactor }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="可能导致事故类型" prop="accidentType" min-width="100">
        <template slot-scope="scope">
          <el-popover placement="top-start" width="400" trigger="hover">
            <div>
              <div v-for="(str, index) in str2List(scope.row.accidentType)" :key="index">
                {{ index + 1 }}、{{ str }}
              </div>
            </div>
            <div class="text-overflow-ellipsis pointer" slot="reference">{{ scope.row.accidentType }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="主要管控措施" prop="controlMeasure" min-width="100">
        <template slot-scope="scope">
          <el-popover placement="top-start" width="800" trigger="hover">
            <div>
              <div v-for="(str, index) in str2List(scope.row.controlMeasure)" :key="index">
                {{ index + 1 }}、{{ str }}
              </div>
            </div>
            <div class="text-overflow-ellipsis pointer" slot="reference">{{ scope.row.controlMeasure }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="所在平台" prop="hostPlatform" width="130" />
      <el-table-column label="开工日期" prop="workStartTime" width="170" />
      <el-table-column label="填报日期" prop="fillTime" width="170" />
      <el-table-column label="施工内容（简述）" prop="buildTime" align="center">
        <el-table-column label="06:00-16:00" prop="buildTimeOne" width="100">
          <template slot-scope="scope">
            <el-popover placement="top-start" width="400" trigger="hover">
              <div>{{ scope.row.buildTimeOne }}</div>
              <div class="text-overflow-ellipsis pointer" slot="reference">{{ scope.row.buildTimeOne
              }}</div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="16:00-24:00" prop="buildTimeTwo" width="100">
          <template slot-scope="scope">
            <el-popover placement="top-start" width="400" trigger="hover">
              <div>{{ scope.row.buildTimeTwo }}</div>
              <div class="text-overflow-ellipsis pointer" slot="reference">{{ scope.row.buildTimeTwo
              }}</div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="00:00-06:00" prop="buildTimeThree" width="100">
          <template slot-scope="scope">
            <el-popover placement="top-start" width="400" trigger="hover">
              <div>{{ scope.row.buildTimeThree }}</div>
              <div class="text-overflow-ellipsis pointer" slot="reference">{{ scope.row.buildTimeThree
              }}
              </div>
            </el-popover>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="下步工序" prop="nextStep" min-width="100" />
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />



    <div slot="footer" class="dialog-footer" style="text-align: center;">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="handleSelectUser">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Api from '@/api/business/risk/report'
import { cloneDeep } from "lodash"
export default {
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      visible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dataList: [],
      // 弹出层标题
      title: '',

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wellNo: undefined,
        wellType: undefined,
        projectName: undefined,
        workStartTime: undefined,
      },
      multipleSelection: [],
      selected: []
    };
  },
  methods: {
    // 显示弹框
    show(selected = []) {
      this.visible = true;
      this.multipleSelection = selected
      this.selected = selected
      this.resetForm('queryForm')
      this.getList()
    },

    close() {
      this.visible = false
    },

    str2List(str) {
      try {
        return str ? str.split("|") : []
      } catch (e) {
        return []
      }
    },

    handleSelectUser() {
      const selected = cloneDeep(this.selected).filter(o => typeof o === 'object')
      // 去重
      const ids = [...new Set(selected.map(item => item.id))]

      this.$emit("select", ids.map(id => selected.find(o => o.id == id)))
      this.$refs.reportTable.clearSelection()
      this.$nextTick(() => {
        this.visible = false
      })
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
      const list = this.dataList.map(item => {
        item.checked = val.some(o => o.id == item.id)
        return item
      })

      this.selected = [...this.selected, ...val].filter(item => {
        const o = list.find(o => o.id == item.id)
        return (o && o.checked) || o === undefined
      })
    },

    /** 查询用户列表 */
    getList() {
      this.loading = true
      const selected = cloneDeep(this.selected)
      Api.query(this.addDateRange(this.queryParams)).then(response => {
        this.dataList = response.data
        this.total = response.total
        this.loading = false
        this.$nextTick(() => {
          response.data.forEach(row => {
            if (selected.some(data => data == row.id || data.id == row.id)) {
              this.$refs.reportTable.toggleRowSelection(row, true);
            } else {
              this.$refs.reportTable.toggleRowSelection(row, false);
            }
          })
        })
      }
      )
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
  }
};
</script>
